package com.reshare.controller;

import com.reshare.annotation.Log;
import com.reshare.base.BaseController;
import com.reshare.base.ResultInfo;
import com.reshare.bean.Commodity;
import com.reshare.bean.CommodityImg;
import com.reshare.model.CommodityModel;
import com.reshare.query.CommodityQuery;
import com.reshare.service.CommodityService;
import com.reshare.utils.AssertUtil;
import com.reshare.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/*功能描述
 * @author ahm
 */
@Controller
@RequestMapping("commodity")
public class CommodityController extends BaseController {

    @Autowired
    private CommodityService commodityService;


    @RequestMapping("toCommodity")
    public String toCommodity(){
        return "commodity/commodity";
    }
///commodity/commodityList
    @RequestMapping("commodityList")
    @ResponseBody
    public Map<String,Object> findAllCommodity(CommodityQuery commodityQuery){
        return commodityService.findCommodityList(commodityQuery);
    }

    @RequestMapping("toAdd")
    public String toAdd(){

        return "commodity/add";

    }
//    toCommodityDetail    content : ctx+'/commodity/toCommodityDetail?id=' + commodityId, //这里content是一个Url
    /**
     * 跳转到商品详情页面
     * 并查询该商品所有信息，（包括所有图片），返回
     */
    @RequestMapping("/toCommodityDetail")
    public ModelAndView toCommodityDetail(Integer id){
        ModelAndView mav = new ModelAndView();
        // 获取商品文本信息
        Commodity commodity = commodityService.findById(id);
        // 获取商品所有图片信息
        List<CommodityImg> commodityImgList = commodity.getCommodityImgList();
        for (CommodityImg commodityImg : commodityImgList) {
            System.out.println(commodityImg.getImgSrc());
        }
        mav.addObject("commodity",commodity);
        mav.addObject("commodityImgList",commodityImgList);
        mav.addObject("id",id);

        mav.setViewName("commodity/commodityDetail");
        return mav;
    }


//    url: '/commodity/addCommodity',
    /*
    *
    * 添加商品
    * */

    @RequestMapping("addCommodity")
    @ResponseBody
    public ResultInfo addCommodity(@RequestParam("commodityImgs") MultipartFile[] multipartFiles, HttpServletRequest request, Commodity commodity, @RequestParam("categoryId") Integer categoryId){

        commodityService.addCommodity(multipartFiles,request,commodity,categoryId);
        return success("添加成功");
    }


    /*
    * 修改商品
    *
    url:'/commodity/delCommodity',  /commodity/delCommodity
    data:{"commodityId":commodityId,"userId":userId},
    * */
   @RequestMapping("delCommodity")
   @Log("下架商品")
   @ResponseBody
    public ResultInfo delCommodity(@RequestParam("commodityId") Integer commodityId,@RequestParam("userId")Integer userId ){

       commodityService.delCommodity(commodityId,userId);

       return success("下架成功");
    }




}
